package com.power.sifa.approve.dao;

import com.power.sifa.approve.model.Apl_admre_audit;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;

/**
 * 行政复议相关审批表 持久层(hibernate)
 */

public interface Apl_admre_auditDao extends JpaRepository<Apl_admre_audit, Serializable>, JpaSpecificationExecutor<Apl_admre_audit>, Repository<Apl_admre_audit, Serializable>, Apl_admre_auditDaoCustom<Apl_admre_audit, Serializable> {

    @Modifying
    @Transactional
    @Query("delete  from Apl_admre_audit s where  s.id in (:ids) ")
    void removeByIds(@Param("ids") List<Integer> ids);

    /**
     * 更新业务主表状态
     *
     * @param id
     * @param status
     */
    @Modifying
    @Transactional
    @Query("update Apl_admre_audit s set s.status = :status where  s.id = :id ")
    void updateStatus(@Param("id") String id, @Param("status") Integer status);

    /**
     * 更新被申请人是否已答复
     *
     * @param applyid       父流程applyid
     * @param istargetreply
     */
    @Modifying
    @Transactional
    @Query("update Apl_admre_audit s set s.istargetreply = :istargetreply where  s.applyid = :applyid ")
    void updateIstargetreply(@Param("applyid") String applyid, @Param("istargetreply") Integer istargetreply);

    /**
     * 更新是否中止
     *
     * @param applyid 父流程applyid
     * @param isrest
     */
    @Modifying
    @Transactional
    @Query("update Apl_admre_audit s set s.isrest = :isrest where  s.applyid = :applyid ")
    void updateIsrest(@Param("applyid") String applyid, @Param("isrest") Integer isrest);

    /**
     * 更新程序类型
     *
     * @param applyid  父流程applyid
     * @param proctype
     */
    @Modifying
    @Transactional
    @Query("update Apl_admre_audit s set s.proctype = :proctype where  s.applyid = :applyid ")
    void updateProctype(@Param("applyid") String applyid, @Param("proctype") Integer proctype);

    /**
     * 根据applyid查询列表
     *
     * @param applyid
     * @return
     */
    @Query("from Apl_admre_audit s where s.applyid = :applyid ")
    List<Apl_admre_audit> getListByApplyid(String applyid);


    /**
     * 获取最新一条子流程记录
     *
     * @param parentApplyid
     * @param proctype
     * @return
     */
    @Query(value = "select * from (select t.*, row_number() over( partition by type order by id desc) rk from apl_admre_audit t where applyid = :parentApplyid and type = :proctype)  where rk = 1 ", nativeQuery = true)
    Apl_admre_audit getLastChildAudit(@Param("parentApplyid") String parentApplyid, @Param("proctype") Integer proctype);

    /**
     * 更新业务从表期满日期
     *
     * @param applyid
     * @param quedate
     */
    @Modifying
    @Transactional
    @Query("update Apl_admre_audit s set s.quedate = :quedate where  s.applyid = :applyid ")
    void updateQuedate(String applyid, String quedate);

}
